package com.gouda.core.service.impl.order;

import com.google.common.collect.Lists;
import com.gouda.core.dao.OrderDao;
import com.gouda.core.dto.order.OrderQueryDto;
import com.gouda.core.dto.order.OrderQueryParamDto;
import com.gouda.core.entity.OrderEntity;
import com.gouda.core.service.order.OrderService;
import com.hephaestus.utils.DataUtil;
import com.themis.constant.FrontConstant;
import org.amethyst.atlas.basic.component.PageData;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

/**
 * Created by xuxiaoai on 2015/12/8.
 */
public class OrderServiceImpl implements OrderService {
    @Autowired
    private OrderDao orderDaoDao;

    @Override
    public PageData<OrderQueryDto> getOrderByPage(OrderQueryParamDto orderQueryParamDto) {
        final int count = orderDaoDao.getCountByConditions(orderQueryParamDto);
        if (count <= 0) {
            return null;
        }
        int queryPageNum = FrontConstant.DEFAULT_START_PAGE;
        if (null != orderQueryParamDto && null != orderQueryParamDto.getPageNumber()) {
            queryPageNum = orderQueryParamDto.getPageNumber();
        }
        int pageSize = FrontConstant.DEFAULT_PAGE_SIZE;
        if (null != orderQueryParamDto && null != orderQueryParamDto.getPageSize()) {
            pageSize = orderQueryParamDto.getPageSize();
        }

        PageData<OrderQueryDto> pageData = new PageData<>(count, queryPageNum, pageSize);
        List<OrderEntity> orderList = orderDaoDao.getListByConditions(orderQueryParamDto, pageData.getSqlStart(), pageData.getRowsPerPage());

        pageData.setData(this.generateDtos(orderList));
        return pageData;
    }

    private List<OrderQueryDto> generateDtos(final List<OrderEntity> orderList) {
        List<OrderQueryDto> dtos = Lists.newArrayList();
        for (OrderEntity orderEntity : orderList) {
            OrderQueryDto dto = new OrderQueryDto();
            dto.setId(orderEntity.getId());
            dto.setServiceType(orderEntity.getServiceType());
            dto.setUid(orderEntity.getUid());
            dto.setMerchant(orderEntity.getMerchant());
            dto.setOrderCode(orderEntity.getOrderCode());
            dto.setUcId(orderEntity.getUcId());
            dto.setOrderType(orderEntity.getOrderType());
            if (null != orderEntity.getReserveTime()) {
                dto.setReserveTime(DataUtil.formatDate(orderEntity.getReserveTime()));
            }
            dto.setExpectPrice(orderEntity.getExpectPrice());
            dto.setDeratePrice(orderEntity.getDeratePrice());
            if (null != orderEntity.getResponseTime()) {
                dto.setResponseTime(DataUtil.formatDate(orderEntity.getResponseTime()));
            }
            dto.setExpectServiceTime(orderEntity.getExpectServiceTime());
            dto.setServiceTime(orderEntity.getServiceTime());
            dto.setEndServiceTime(DataUtil.formatDate(orderEntity.getEndServiceTime()));
            dto.setTotalPrice(orderEntity.getTotalPrice());
            dto.setActualPaymentPrice(orderEntity.getActualPaymentPrice());
            dto.setServiceNumber(orderEntity.getServiceNumber());
            if (null != orderEntity.getResponseTime()) {
                dto.setPayTime(DataUtil.formatDate(orderEntity.getPayTime()));
            }
            dto.setRemark(orderEntity.getRemark());
            dto.setState(orderEntity.getState());
            dto.setCloseReason(orderEntity.getCloseReason());
            dto.setCreateTime(DataUtil.formatDate(orderEntity.getCreateTime()));
            dtos.add(dto);
        }
        return dtos;
    }
}
